====== SimpleSAMLphp einrichten ======

SimpleSAMLphp wurde auf webservices eingerichtet.

TODO: in config/config.php ists debugging eingeschalten, muss später wieder aus:
- debug -> saml und xml von true auf false
- loggin level - von debug auf notice

Nach der Installation ist der Login möglich unter\\
https://www.soscisurvey.de/shibboleth/module.php/core/frontpage_welcome.php

==== Download u. Installation ====

<code bash>
sudo apt update
sudo apt install php-xml php-mbstring php-curl php-memcache php-ldap memcached php-sqlite3

sudo mkdir /var/shibboleth
cd /var/shibboleth
sudo wget -O simplesaml https://simplesamlphp.org/download?latest
sudo tar xzf simplesaml
sudo rm simplesaml

ls -la
sudo ln -s simplesamlphp-1.19.1 simplesaml
</code>

=== Metarefresh ===

<code bash>
chown -R sosci:root cd /var/shibboleth/simplesaml
cd /var/shibboleth/simplesaml
composer require simplesamlphp/simplesamlphp-module-metarefresh
</code>

==== Apache einrichten ====

In ''/etc/httpd/conf/httpd.conf'' bzw. in der entsprechenden Site-Konfiguration unter ''/etc/apache2/sites-available/'' hinzufügen: 

<code>
SetEnv SIMPLESAMLPHP_CONFIG_DIR /var/shibboleth/simplesaml/config
Alias /shibboleth /var/shibboleth/simplesaml/www
<Directory /var/simplesamlphp/www>
    <IfModule mod_authz_core.c>
    Require all granted
    </IfModule>
</Directory>
</code>

Beziehungsweise wenn ''nginx'' zum Einsatz kommt:

<file conf /etc/nxinx/sites-available/whatever.conf>
        location ^~ /shibboleth/ {
                alias /var/shibboleth/simplesaml/www/;

                location ~ ^/shibboleth/(?<phpfile>.+?\.php)(?<pathinfo>/.*)?$ {
                        fastcgi_pass sosci;
                        fastcgi_index index.php;
                        fastcgi_split_path_info ^/shibboleth/(.+?\.php)(.*)$;
                        fastcgi_param SCRIPT_FILENAME  /var/shibboleth/simplesaml/www/$phpfile;
                        fastcgi_param PATH_INFO $pathinfo if_not_empty;
                        fastcgi_param SIMPLESAMLPHP_CONFIG_DIR /var/shibboleth/config;
                        include fastcgi_params;
                        fastcgi_param PHP_VALUE "
                                max_execution_time = 5
                                memory_limit = 64M
                                max_file_uploads = 5
                                post_max_size = 16M
                        ";
                }
        }
</file>

Und eventuell ist dann noch notwendig...

<file conf /etc/php/7.4/fpm/pool.d/primary.conf>
[www]
env[SIMPLESAMLPHP_CONFIG_DIR] = /var/shibboleth/config
</file>

SELinux muss mit der aktuellen SAML-Version u.U. noch deaktiviert werden in ''/etc/sysconfig/selinux''.
==== SimpleSAML einrichten ====

Das Verzeichnis ''shibboleth/simplesaml/config/'' wird kopiert (!) nach ''shibboleth/config/''.

<code bash>
cs /var/shibboleth
sudo cp -r simplesaml/config config

sudo mkdir log
sudo mkdir metadata
sudo mkdir cert
sudo chown www-data:root log
sudo chown www-data:root metadata

sudo touch simplesaml/modules/cron/enable
sudo touch simplesaml/modules/metarefresh/enable
</code>

Dann aktualisieren in ''/var/shibboleth/config/config.php''

  * 'baseurlpath' => 'https://webservices2020.ifkw.lmu.de/shibboleth/',
  * 'auth.adminpassword' geändert (im Keepass unter Webservices: SimpleSAMLphp)
  * 'secretsalt' geändert
  * 'technicalcontact_email'    => 'helpdesk@ifkw.lmu.de'
  * 'language.default'      => 'de'
  * 'timezone' => 'Europe/Berlin'

Außerdem noch die Pfade weil wir die Konfiguration ja in einem separaten Verzecihnis haben:

<code>
    'certdir' => __DIR__.'/../cert/',
    'loggingdir' => __DIR__.'/../log/',
    'datadir' => 'data/',
    'tempdir' => __DIR__.'/../tmp/',
    'metadatadir' => __DIR__.'/../metadata/',
</code>

In ''config/authsources.php'' wird ''%%'idp' => null%%'', zu ''%%'idp' => 'https://lmuidp.lrz.de/idp/shibboleth'%%'' geändert.

 

==== Zertifikate ====
Zertifikate in SAML kopieren:
<code>cp /etc/pki/tls/certs/201707-webservices.crt /var/simplesamlphp/cert/saml.crt
cp /etc/pki/tls/private/201707-webservices.key /var/simplesamlphp/cert/saml.pem</code>und in /var/simplesamlphp/config/authsources.php folgende Zeilen ins Array 'default-sp' hinzufügen (default-sp soll als SP nicht genutzt werden, die Zeilen dienen aber gut als Kopiervorlage für andere SPs):<code>'privatekey' => 'saml.pem',
'certificate' => 'saml.crt',</code>

==== Service Provider anlegen ====
Auf unserem Server können mehrere SPs angelegt werden (pro Webdienst ein SP), die alle unter der selben Installation von SimpleSAMLphp laufen. Wie sie angelegt und eingerichtet werden können, steht unter [[simplesamlphp:addsp|SP einrichten]].

==== Metadaten automatisiert anpassen ====
Zwischen IdP und SP werden Metadaten ausgetauscht. Diese müssen immer übereinstimmen (meist ist das Problem nur ein 'expire'), daher holen wir uns die Metadaten von der DFN-AAI. Das muss nur einmal eingerichtet werden, aber später für weitere SP noch Zeilen hinzugefügt werden.

  * Die Module cron und metafresh müssen aktiviert werden. Metafresh aktualisiert die Metadaten und cron führt metafresh aus.<code>
cd /var/...
touch simplesaml/modules/cron/enable
cp simplesaml/modules/cron/config-templates/*.php config/
touch simplesaml/modules/metarefresh/enable
cp simplesaml/modules/metarefresh/config-templates/*.php config/
</code>

  * Metafresh konfigurieren\\ In //config/config-metarefresh.php// werden die Einstellungen für Metafresh vorgenommen. Das Modul holt sich automatisch die Metadaten des DFN-AAI ('src') und parst sie in die entsprechenden Dateien. Da in diesen Metadaten auch diejenigen von fremden SPs enthalten sind, nutzen wir die White List und parsen nur die Entitäten, die wir auch wirklich benötigen. Hier werden alle zusätzlichen SP eingetragen.<code><?php

$config = array(
  'sets' => array(
    'aai' => array(
      'cron' => array('frequent'),
      'sources' => array(
        array(

        // Whitelist: only keep these EntityIDs.
        'whitelist' => array(
          'https://webservices.ifkw.lmu.de/simplesaml/module.php/saml/sp/metadata.php/default-sp',
          'https://webservices.ifkw.lmu.de/simplesaml/module.php/saml/sp/metadata.php/stellenboerse',
          'https://webservices.ifkw.lmu.de/simplesaml/module.php/saml/sp/metadata.php/kobe',
          'https://webservices.ifkw.lmu.de/simplesaml/module.php/saml/sp/metadata.php/mrbs',
          'https://lmuidp.lrz.de/idp/shibboleth',
        ),

        #'src' => 'http://www.aai.dfn.de/fileadmin/metadata/DFN-AAI-Local-29-metadata.xml',
        'src' => 'https://www.aai.dfn.de/fileadmin/metadata/dfn-aai-local-29-metadata.xml',
      ),
  ),
  'expireAfter' => 60*60*24*4, // Maximum 4 days cache time
  'outputDir' => 'metadata/',
  'outputFormat' => 'flatfile',
  ),
  ),
);</code> 
  * Logoutnachrichten signieren.\\ Aus dem geparsten XML erhalten wir jedoch nicht den Befehl, der die Logout-Nachrichten signiert. Der muss aber immer in den Metadaten enthalten sein. Daher wird der Metafresh-Code minimal angepasst:\\ Datei ///simplesamlphp/modules/metarefresh/lib/MetaLoader.php//; Funktion //addMetadata//\\ <code> Z. 330   $metadata['sign.logout'] = true;
</code> 
  * Schreibrechte erteilen\\ Der Webserver benötigt noch Schreibrechte für den outputDir-Ordner:<code>
chown www-data /var/simplesaml/metadata/</code>
  * Crobtab anlegen\\ Damit Metafresh auch ausgeführt wird, muss ein Crontab angelegt werden:<code>
crontab -u www-data -e (Hier Webserver-Nutzername einfügen)
</code> Da wird dann eingetragen (einmal stündlich aktualisieren): <code>
0 * * * * curl --silent "https://webservices.ifkw.lmu.de/simplesaml/module.php/cron/cron.php?key=SECRET_PASSWORD&tag=frequent" > /dev/null 2>&1
2 0 * * * curl --silent "https://webservices.ifkw.lmu.de/simplesaml/module.php/cron/cron.php?key=SECRET_PASSWORD&tag=daily" > /dev/null 2>&1
 </code>
  * Das Passwort (also nicht ''SECRET_PASSWORD'') noch ins KeePass schreiben und in ''config/module_cron.php'' eintragen.


==== Reports per Email unterbinden ====
Damit keine "SimpleSamlPHP cron reports" mehr per Email kommen, muss diese Einstellung in der Config des "cron" Modules von SimpleSaml deaktivieren.
Hierzu unter: /var/simplesamlphp/config/module_cron.php die Zeile "sendemail = false" lauten. 

==== SimpleSAML konfigurieren ====

Damit SimpleSAML mit SoSci Survey zusammenarbeitet, müssen in der ''simplesamlphp/config/config.php'' die Session-Parameter noch auf SoSci Survey abgestimmt werde:

<code php>
    'session.phpsession.cookiename' => 'PHPSESSID',
    'session.phpsession.savepath' => '/var/www/s2survey/html/system/session',
</code>


===== Probleme lösen =====

Zunächst einmal muss der Metadata Refresh funktionieren. Man kann ihn über die SimpleSAML-Oberfläche manuell auslösen. Das Zertifikat und der private Schlüssel müssen von ''www-data'' lesbar sein. Aus diesem Grund könnte es sinnvoll sein, für Shibboleth ein separates Zertifikat zu erzeugen.

SimpleSAMLPHP speichert Logfiles standardmäßig ins ''syslog''. Wenn man möchte, dass sie unter ''/shibboleth/log'' landen, muss man in der ''config.php'' die folgende Zeile ändern: '''logging.handler' => 'file',''

Wenn im Errorlog etwas steht wie folgt, dann ändert das PHP-Script vermutlich den Pfad, wo Sessions gespeichert werden. Den passenden Pfad muss man dann auch in der ''config.php'' von SimpleSAMLPHP eintragen, z.B. '''session.phpsession.savepath' => '/var/www/s2survey/html/system/session',''.


===== Doppeltes abgleichen und entfernen =====


Die Konfiguration der Umgebungsvariable für ''config'' haben wir in der ''/etc/php/8.1/fpm/pool.d/primary.conf'' erledigt. In der ''nginx''-Konfiguration ist es auch möglich, im Moment aber nur für das ''/shibobleth/''-Verzeichnis aktiv.

<file conf /etc/php/8.1/fpm/pool.d/primary.conf>
env[SIMPLESAMLPHP_CONFIG_DIR] = /var/www/shibboleth/config
</file>


===== Installation =====

TAR.GZ runterladen s.u.

<code conf>
        # SimpleSAML / Shibboleth
        location ^~ /shibboleth/ {
                alias /var/www/shibboleth/simplesaml/www/;

                location ~ ^/shibboleth/(?<phpfile>.+?\.php)(?<pathinfo>/.*)?$ {
                        fastcgi_pass sosci;
                        fastcgi_index index.php;
                        fastcgi_split_path_info ^/shibboleth/(.+?\.php)(.*)$;
                        fastcgi_param SCRIPT_FILENAME  /var/www/shibboleth/simplesaml/www/$phpfile;
                        fastcgi_param PATH_INFO $pathinfo if_not_empty;
                        fastcgi_param SIMPLESAMLPHP_CONFIG_DIR /var/www/shibboleth/config;
                        include fastcgi_params;
                        fastcgi_param PHP_VALUE "
                                max_execution_time = 5
                                memory_limit = 64M
                                max_file_uploads = 5
                                post_max_size = 16M
                        ";
                }
        }
</code>

Konfigurationsverzeichnis

<code bash>
cd /var/www/shibboleth
sudo cp -r simplesaml/config config
 
sudo mkdir log
sudo mkdir metadata
sudo mkdir cert
sudo chown www-data:root log
sudo chown www-data:root metadata
 
# sudo touch simplesaml/modules/cron/enable
# sudo touch simplesaml/modules/metarefresh/enable
</code>

<file config config.php>
    'module.enable' => [
        'exampleauth' => false,
        'core' => true,
        'admin' => true,
        'saml' => true,
        'cron' => true,
        'metarefresh' => true,
    ],
</file>

Dann aktualisieren in ''/var/shibboleth/config/config.php''

  * 'baseurlpath' => 'https://webservices2020.ifkw.lmu.de/shibboleth/',
  * 'auth.adminpassword' geändert (im Keepass unter Webservices: SimpleSAMLphp)
  * 'secretsalt' geändert
  * 'technicalcontact_email'    => 'helpdesk@ifkw.lmu.de'
  * 'language.default'      => 'de'
  * 'timezone' => 'Europe/Berlin'

Außerdem noch die Pfade weil wir die Konfiguration ja in einem separaten Verzecihnis haben:

<code>
    'certdir' => __DIR__.'/../cert/',
    'loggingdir' => __DIR__.'/../log/',
    'datadir' => 'data/',
    'tempdir' => __DIR__.'/../tmp/',
    'metadatadir' => __DIR__.'/../metadata/',
</code>

In ''config/authsources.php'' wird ''%%'idp' => null%%'', zu ''%%'idp' => 'https://lmuidp.lrz.de/idp/shibboleth'%%'' geändert.

Ein paar Zertifikate

<code bash>
openssl req -newkey rsa:2048 -new -x509 -days 3652 -nodes -text -out cert/saml.crt -keyout cert/saml.key
</code>

<code bash>
vim config/authsources.php
</code>

<file>
    'default-sp' => [
        'saml:SP',
        'privatekey' => 'saml.key',
        'certificate' => 'saml.crt',
</file>

Falls es einen Fehler "InvalidNameIDPolicy" gibt, dann noch ergänzen (https://stackoverflow.com/a/51380264/336311).

<code conf>
       'NameIDPolicy' => NULL
</code>

Die Identitiy Provider werden in der ''config/config.php'' und in der ''config/config-metarefresh.php'' eingetragen. Siehe dazu auch [[:de:internal:sso]].

<file conf config/config.php>
    'metadata.sources' => [
        ['type' => 'flatfile'],
        ['type' => 'flatfile', 'directory' => '/var/www/shibboleth/metadata/metarefresh-PARTNER']
    ],
</file>

<file conf config/config-metarefresh.php>
<?php

$config = [

        'sets' => [

                'PARNER' => [
                        'cron'          => array('hourly'),
                        'sources'       => array(
                                array(
                                        'src' => 'https://PARTNER-URL/idp/shibboleth',
                                        'types' => array('saml20-idp-remote', 'shib13-idp-remote'),
                                ),
                        ),
                        'expireAfter' => 60*60*24*4, // Maximum 4 days cache time
                        'outputDir' => 'metadata/metarefresh-PARTNER/',
                        'outputFormat' => 'flatfile',
                        'types' => array('saml20-idp-remote', 'shib13-idp-remote'),
                ]
        ]
];
</file>


==== Update ====

https://simplesamlphp.org/download -> URL
https://simplesamlphp.org/download?latest

Upgrade laut [[https://simplesamlphp.org/docs/stable/simplesamlphp-install#section_3|Anleitung]] mit kleinen Optimierungen.

<code bash>
cd /var/www/shibboleth
sudo wget https://simplesamlphp.org/download?latest
sudo tar -xvf "download?latest"
sudo rm "download?latest"

# Die Konfiguration liegt jetzt direkt in /var/www/shibboleth/config/
# cd /var/simplesamlphp-x.y.z
# sudo rm -rf config metadata
# sudo cp -rv ../simplesaml/config config
# sudo cp -rv ../simplesaml/metadata metadata
# sudo cp -rv ../simplesaml/cert cert

# Änderungen abgleichen
cd /var/www/shibboleth
diff -u simplesamlphp-2.0.2/config/config.php.dist config/config.php

sudo rm simplesaml
sudo ln -s simplesamlphp-1.NEUESTE simplesaml

cd /var/www/shibboleth
composer require simplesamlphp/simplesamlphp-module-metarefresh

# Eventuell davor
apt install composer php-sqlite3
</code>

Zum Aktivieren des Meta-Refresh s. https://simplesamlphp.org/docs/contrib_modules/metarefresh/simplesamlphp-automated_metadata.html